import { defineStore } from 'pinia';
import { Notification } from '@arco-design/web-vue';
import type { NotificationReturn } from '@arco-design/web-vue/es/notification/interface';
import type { RouteRecordNormalized } from 'vue-router';
import defaultSettings from '@/config/settings.json';
import { getMenuList } from '@/api/user';
import { AppState } from './types';
import { DEFAULT_LAYOUT } from '@/router/constans';
import api from '@/api/sys/LuYou'

const useAppStore = defineStore('app', {
  state: (): AppState => ({ ...defaultSettings }),

  getters: {
    appCurrentSetting(state: AppState): AppState {
      return { ...state };
    },
    appDevice(state: AppState) {
      return state.device;
    },
    appAsyncMenus(state: AppState): RouteRecordNormalized[] {
      return state.serverMenu as unknown as RouteRecordNormalized[];
    },
  },

  actions: {
    // Update app settings
    updateSettings(partial: Partial<AppState>) {
      // @ts-ignore-next-line
      this.$patch(partial);
    },

    // Change theme color
    toggleTheme(dark: boolean) {
      if (dark) {
        this.theme = 'dark';
        document.body.setAttribute('arco-theme', 'dark');
      } else {
        this.theme = 'light';
        document.body.removeAttribute('arco-theme');
      }
    },
    toggleDevice(device: string) {
      this.device = device;
    },
    toggleMenu(value: boolean) {
      this.hideMenu = value;
    },
    async fetchServerMenuConfig() {
      let notifiInstance: NotificationReturn | null = null;
      try {
        // notifiInstance = Notification.info({
        //   id: 'menuNotice', // Keep the instance id the same
        //   content: 'loading',
        //   closable: true,
        // });
        var { data } = await getMenuList();
        var arr = undefined
        if (!localStorage.getItem("router")&&localStorage.getItem("token")) {
          await api.findByIdRouting().then(res => {
           // console.log(res);
            arr = res.data.data.data
            localStorage.setItem("router", JSON.stringify(arr))
          })
        } else {
          if(localStorage.getItem("router")){
            arr = JSON.parse(localStorage.getItem("router")||'') 
          }
      
        }
        var arrs=[]
        var routerss=[
          {
            path: '/user',
            name: 'user',
            component: DEFAULT_LAYOUT,
            meta: {
              locale: 'menu.user',
              icon: 'icon-user',
              requiresAuth: true,
              hideInMenu:true,
              show: true   ,
              order: 7,
            },
            children: [
              {
                path: 'info',
                name: 'Info',
                component: () => import('@/views/user/info/index.vue'),
                meta: {
                  locale: 'menu.user.info',
                  hideInMenu:true,
                  requiresAuth: true,
                  roles: ['*'],
                },
              },
              {
                path: 'setting',
                name: 'Setting',
                component: () => import('@/views/user/setting/index.vue'),
                meta: {
                  locale: 'menu.user.setting',
                  hideInMenu:true,
                  requiresAuth: true,
                  roles: ['*'],
                },
              },
            ],
          }
        ]
        if(arr!=undefined)arrs = [...arr, ...data,...routerss]
        else arrs=data
        this.serverMenu = arrs;

        // notifiInstance = Notification.success({
        //   id: 'menuNotice',
        //   content: 'success',
        //   closable: true,
        // });
      } catch (error) {
        console.log(error);
        
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        notifiInstance = Notification.error({
          id: 'menuNotice',
          content: 'error',
          closable: true,
        });
      }
    },
  },
});

export default useAppStore;
